package com.bsoft.core.serialization;

import org.nustaq.serialization.FSTConfiguration;

/**
 * 扩展：通过@Version标识字段版本，实现新字段扩展
 */
public class FSTSerialization implements Serialization {
    /**
     * FSTConfiguration是线程安全的，为了防止频繁调用成为性能瓶颈，通过线程私有来为每个线程分配一个FSTConfiguration
     */
    private final ThreadLocal<FSTConfiguration> conf = ThreadLocal.withInitial(() -> {
        FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();

        // 优化
        //conf.setShareReferences(false);// 关闭循环引用
        //conf.registerClass(Person.class);// 预注册序列化类
        return conf;
    });

    @Override
    public <T> byte[] serialize(T obj) {
        return conf.get().asByteArray(obj);
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clz) {
        Object ob = conf.get().asObject(data);
        return (T)ob;
    }





}
